// home/notice/Notice.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
import notification from '../../utils/notification_center.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    animation: false,
    finish: true,
    details: null,
    userInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let param = {
      id: options.id
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      param.adminid = app.globalData.userInfo.uid;
      param.token = app.globalData.userInfo.token;
    }
    network.postJson(this, api.gonggaoDetails, param).then(res => {
      res.data.data.content = formatImg(res.data.data.content);
      this.setData({
        details: res.data.data
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.details.title,
      path: '/policy/policy/PolicyDetails?id=' + this.data.details.id
    }
  },

  goHome: function () {
    wx.reLaunch({
      url: '/pages/main/Home'
    })
  },
  clickImg:function(e){
    let that=this
    let arr=[];
    //let reg = /(?<=(src="))[^"]*?(?=")/ig
    // 发现一个Bug小程序js打包后不支持原生js双斜杠的方式,所以替换一下
    let reg = new RegExp('(?<=(src="))[^ "]*?(?=")','ig')
    let allSrc = that.data.details.content.match(reg)
    for (let i = 0; i<allSrc.length;i++){
      arr.push(allSrc[i])
      that.setData({
        imageArray:arr
      })
    }
    setTimeout(function(){
      wx.previewImage({
        current: that.data.imageArray, // 当前显示图片的http链接   
        urls: that.data.imageArray // 需要预览的图片http链接列表   
      }) 
    },500)
  }

})

var success = function (that, res) {
  if (res.data.status == 1) {
    res.data.data.createDate = utils.formatDate07(new Date(res.data.data.createDate.replace(/-/g, '/')));
    if (res.data.data.type == "notice") {
      res.data.data.typeId = "公示公告"
    }
    that.setData({
      news: res.data.data,
      // detail: res.data.data.detail.replace(/style=\"(.*)\"/gi, '').replace(/\<img/gi, '<img style="width:100%;height:auto; display: -webkit - flex; display: flex; flex-direction: column;"')
      detail: formatImg(res.data.data.detail)
    })
    if (res.data.data.isCollect == 1) {
      that.setData({
        collecth: true
      })
    } else {
      that.setData({
        collecth: false
      })
    }
  }
}
var formatImg = function (html) {
  var newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
    var match = match.replace(/style=\"(.*)\"/gi, '').replace(/\<img/gi, '<img style="width:100%;height:auto; display: -webkit - flex; display: flex; flex-direction: column;"');
    return match;
  });
  return newContent.replace(/\&nbsp;/gi, '&ensp;');
}